<?php

namespace app\common\model;


use app\common\controller\UserController;
use app\common\helper\util\Crypt;

class User extends Model
{
    public $columns = [
        [  "type"=> self::TYPE_VARCHAR,  "column"=>"user_name",  "memo"=>"用户名", "length"=> 64 ],
        [  "type"=> self::TYPE_VARCHAR,  "column"=>"password",  "memo"=>"密码", "length"=> 64 ],
        [  "type"=> self::TYPE_TINYINT,  "column"=>"status",  "memo"=>"状态", "length"=> 4 ],
        [  "type"=> self::TYPE_TINYINT,  "column"=>"is_admin",  "memo"=>"管理员", "length"=> 4 ],
        [  "type"=> self::TYPE_VARCHAR,  "column"=>"avatar",  "memo"=>"头像", "length"=> 64 ],
        [  "type"=> self::TYPE_TIMESTAMP,  "column"=>"last_login_time",  "memo"=>"上次登录时间", "length"=> 64 ],
        [  "type"=> self::TYPE_TINYINT,  "column"=>"role_id",  "memo"=>"角色id", "length"=> 4 ],
    ];
    function iLoginSuccess(){
        UserController::$session->set("uid",$this->id);
        $this->last_login_time = date("Y-m-d H:i:s");
        $this->save();
    }
    static function loginSuccess(User $user){
        UserController::$session->set("uid",$user->id);
        $user->last_login_time = date("Y-m-d H:i:s");
        $user->save();
    }
    function getDetail($k = null){
        if(empty($k)){
            $data = UserDetail::simpleAll(["uid"=>$this->id,"ukey"=>$k]);
            return $data;
        }
        $item = UserDetail::simpleOne(["uid"=>$this->id,"ukey"=>$k]);
        if($item){
            return $item->uvalue;
        }else{
           return false;
        }
    }
    function setDetail($k,$v){
        $item = UserDetail::simpleOne(["uid"=>$this->id,"ukey"=>$k]);
        if($item){
            $item->uvalue = $v;
            $item->save();
        }else{
            UserDetail::simpleAdd([
                "ukey"=>$k,
                "uvalue"=>$v,
            ]);
        }
    }

    static function logoutSuccess(){
        UserController::$session->set("uid",-1);
        return true;
    }
    static function registerSuccess($param){
        if( !isset( $param["user_name"]) || !isset( $param["password"]) ){
            return false;
        }
        $param["password"] =  Crypt::pwd( $param["password"]);
        $data = \app\common\model\User::simpleAdd($param);
        return $data;
    }
    function getMoney(){
        BEGIN:
        $money = UserMoney::simpleOne(["uid"=>$this->id]);
        if(!$money){
            UserMoney::simpleAdd(["uid"=>$this->id]);
            goto BEGIN;
        }
        return $money;
    }
    function getMsg(){
        $message = UserMsg::simpleList([
            "query"=>function($query){
                $query->where(["uid"=>$this->id]);
            },
            "result"=>function(&$row){
                if($this->id =  $row["from_uid"]){
                    $row["from"] = $this;
                }else{
                    $row["from"] = User::getById($row["from_uid"]);
                }
                if($this->id =  $row["to_uid"]){
                    $row["to"] = $this;
                }else{
                    $row["to"] = User::getById($row["to_uid"]);
                }
            }
        ]);
        return $message;
    }
    function getPayment(){
        $message = Payment::simpleList([
            "query"=>function($query){
                $query->where(["uid"=>$this->id]);
            },
            "result"=>function(&$row){

            }
        ]);
        return $message;
    }
    function getAttachment(){
        $data = UserAttachment::simpleList([
            "query"=>function($query){
                $query->where(["uid"=>$this->id]);
            },
            "result"=>function(&$row){
            }
        ]);
        return $data;
    }
    function getEmploy(){
        BEGIN:
        $employ = Employ::simpleOne(["uid"=>$this->id]);
        if(!$employ){
            Employ::simpleAdd(["uid"=>$this->id]);
            goto BEGIN;
        }
        return $employ;
    }
    function getServer(){
        BEGIN:
        $iuser = Server::simpleOne(["uid"=>$this->id]);
        if(!$iuser){
            Server::simpleAdd(["uid"=>$this->id]);
            goto BEGIN;
        }
        return $iuser;
    }
    static function getUser(){
        $id = self::getUid();
        return self::getById($id);
    }
    static function getUid(){
        return UserController::$session->get("uid");
    }
}
